package com.jianli.smart.core;

import java.lang.reflect.Field;
import java.util.Map;

import com.jianli.smart.core.annotation.Inject;
import com.jianli.smart.core.utils.ArrayUtil;
import com.jianli.smart.core.utils.ReflectionUtil;

public final class IocHelper {
	static{
		// 获取所有bean与实例之间的映射关系
		Map<Class<?>,Object> beanMap = BeanHelper.getBeanMap();
		if(beanMap != null && !beanMap.isEmpty()){
			for(Map.Entry<Class<?>, Object> beanEntry:beanMap.entrySet()){
				// 分别获取bean与实例
				Class<?> beanClass = beanEntry.getKey();
				Object beanInstance = beanEntry.getValue();
				// 获取Bean的成员变量 getFields无法获取私有
				Field[] fields = beanClass.getDeclaredFields();
				if(ArrayUtil.isNotEmpty(fields)){
					for(Field field:fields){
						// 判断是否带有Inject
						if(field.isAnnotationPresent(Inject.class)){
							// 获取成员变量的实例，并赋值
							Class<?> beanFieldClass = field.getType();
							Object beanFieldInstance = beanMap.get(beanFieldClass);
							if(beanFieldInstance != null){
								ReflectionUtil.setField(beanInstance, field, beanFieldInstance);
							}
						}
					}
				}
			}
		}
		
		
		
		
		
		
		
		
	}
}
